//商品的路由
var express = require('express');
var router = express.Router();

const goodsServie = require('../service/goodsService');

//增加商品
router.post('/', async function (req, res) {
    let data = await goodsServie.insert(req.body);
    res.send(data)
})

//删除商品
router.delete('/:id', async function (req, res) {
    let data = await goodsServie.delete(req.params.id);
    res.send(data)
})

//查询商品
router.get('/', async function (req, res) {
    let { type, value, current, size, adminId } = req.query;
    let searchObj = {};
    if (type && type === "_id") {
        searchObj[type] = value
    } else if (type) {
        searchObj[type] = { $regex: value }
    }
    adminId ? searchObj.adminId = adminId : ""
    let data = await goodsServie.find(searchObj, { current: ~~current || 1, size: ~~size || 20 });
    res.send(data);
})

//修改商品
router.put('/:id', async function (req, res) {
    let data = await goodsServie.update(req.params.id, req.body);
    res.send(data)
})

//查询所有商品(根据门店管理员id)
router.get("/all/:id", async function (req, res) {
    let data = await goodsServie.findAll(req.params.id);
    res.send(data)
})

//获取数据库所有商品
router.get("/all", async function (req, res) {
    let data = await goodsServie.findAll();
    res.send(data)
})

module.exports = router;
